//index.js
//获取应用实例
var App = getApp()
Page({
  data: {
    goods:{},
    prompt: {
      hidden: !0,
      icon: '../../../assets/images/iconfont-order-default.png',
      title: '暂时没有拼团活动'
    }
  },
  onLoad () {
    let agent = wx.getStorageSync('agent')
    let defaultCar = wx.getStorageSync('defaultCar')
    this.setData({agent, defaultCar})
    this.onPullDownRefresh()
  },
  initData () {
    const goods = {
      items: [],
      total: '',
      params: {
        page : 1,
        pageCount: 20
      }
    }
    this.setData({ goods })
  },
  getList (cb) { // 获取拼团商品列表
    let { goods, agent,  defaultCar} = this.data
    let { params } = goods
    params.sellerId = agent.id
    params.carModelId = defaultCar.carModelId
    App.HttpService.getMulti(params).then(res => {
      cb && cb()
      let data = res.data.data
      goods.items = [...goods.items, ...data.multiList]
      if (data.multiList.length > 0) {
        params.page = params.page + 1
        goods.total = data.counts
      }            
      this.setData({ goods, 'prompt.hidden': goods.items.length})
    }, e => {
      cb && cb()
    })
  },
  toDetail (e) {
    let { id } = e.currentTarget.dataset
    let  scene = "229_6391"
    wx.navigateTo({
      url: `/pages/group-buying/detail/index?id=${id}`
    })
  },
  onPullDownRefresh () {
    wx.showNavigationBarLoading()
    this.initData()
    this.getList(() => {
      wx.hideNavigationBarLoading()
      wx.stopPullDownRefresh()
    })
  },
  onReachBottom () {
    let goods = this.data.goods
    if (goods.items.length === goods.total) {
      return
    }
    this.getList()
  },
})
